package byx.container.annotation.util;

import byx.container.annotation.exception.ExtensionLoadException;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class ExtensionLoader {
    public static List<Class<?>> getExtensionClasses(String extensionFile) {
        List<Class<?>> classes = new ArrayList<>();
        try {
            List<URL> urls = JarUtils.getJarResources(extensionFile);
            for (URL url : urls) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
                reader.lines().forEach(line -> {
                    if (!line.trim().startsWith("#") && !line.isBlank()) {
                        try {
                            classes.add(Class.forName(line));
                        } catch (ClassNotFoundException e) {
                            throw new ExtensionLoadException(e);
                        }
                    }
                });
            }
            return classes;
        } catch (Exception e) {
            throw new ExtensionLoadException(e);
        }
    }
}
